/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hill;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

/**
 * @author triasfahrudin
 */
public class Hill3x3Enc extends MIDlet implements CommandListener {

    private Form formMain;
    private Form formResult;
    private Display display;
    private TextField plaintext, matrix;
    private Command enc;
    private String letter[] = {
        "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
        "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
        "U", "V", "W", "X", "Y", "Z"};

    public int findIndex(String s) {
        for (int j = 0; j < letter.length; j++) {
            if(s.equalsIgnoreCase(letter[j])){
                return j;
            }
        }
        return 0;
    }

    public Hill3x3Enc() {
        plaintext = new TextField("Plain text", "", 30, TextField.ANY);
        matrix = new TextField("Matrix", "", 30, TextField.ANY);
        enc = new Command("ENC", Command.OK, 2);
    }

    public void startApp() {
        display = Display.getDisplay(this);
        formMain = new Form("Hill Enc");
        formMain.append(plaintext);
        formMain.append(matrix);
        formMain.addCommand(enc);
        formMain.setCommandListener(this);
        display.setCurrent(formMain);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }

    public void showResult() {
        display = Display.getDisplay(this);
        String sPlainText = plaintext.getString().toUpperCase();
        String sMatrix = matrix.getString();

        //code here

        formResult = new Form("Result");
        formResult.append("Plain Text: " + sPlainText);
        //formResult.append("chiper Text: " + cipherText);
        formResult.addCommand(new Command("Back", Command.BACK, 1));
        formResult.setCommandListener(this);
        display.setCurrent(formResult);
    }

    public void commandAction(Command c, Displayable d) {
        String label = c.getLabel();
        if (label.equals("ENC")) {
            showResult();
        } else if (label.equals("Back")) {
            display.setCurrent(formMain);
        }
    }
}
